package com.songcha.module_splash.ui.dialog

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.songcha.library_business.proxy.configuration.SystemConfigurationManager
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px
import com.songcha.module_splash.R
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.system.exitProcess

class UserPrivacyDialog(context:Context,val mOnConfirm:()->Unit):CustomDialog(context) {
    init {
        setDialogTitle("以下是个人信息保护指引")
            setTitleTextSize(16f)
            setTitleColor(ColorUtil.getResourceColor(context, com.songcha.library_common.R.color.title))
            setTitleMarginTop(50f.dp2px())
            setDialogMsg("")
            setShowBtn(false)
            setWidth(300.dp2px())
            setHeight(460.dp2px())
            setCancelOnTouchOutside(false)
            setCanCancelable(false)
            setShowShadow(true)
            setShadowWidth(10f.dp2px())
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root: ViewGroup): View? {
        val view=layoutInflater.inflate(R.layout.splash_dialog_agreenment,null)
        val tv=view.findViewById<TextView>(R.id.splash_tv)

        val ssb = SpannableString(context.resources.getString(R.string.splash_dialog_user_agreenment))
        val userProtocolClickSpan=object : ClickableSpan(){
            override fun onClick(widget: View) {
                BusinessJumpUtil.toUserPrivacyActivity(widget.context,1)
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color=ColorUtil.getResourceColor(context, com.songcha.library_common.R.color.primary)
                ds.isUnderlineText=false
            }

        }
        val privacyPolicyClickSpan=object : ClickableSpan(){
            override fun onClick(widget: View) {
                BusinessJumpUtil.toUserPrivacyActivity(widget.context,2)
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color=ColorUtil.getResourceColor(context, com.songcha.library_common.R.color.primary)
                ds.isUnderlineText=false
            }
        }

        ssb.setSpan(userProtocolClickSpan, 10, 16, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        ssb.setSpan(privacyPolicyClickSpan, 17, 23, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        tv.movementMethod=(LinkMovementMethod())
        tv.text = ssb


        view.findViewById<CustomButton>(R.id.splash_btn_agreen).setOnClickListener{
            SharedPreferenceHelper.setInt("splash","isAgreen",1)
            //initializeApp()
            mOnConfirm()
            dismiss()
        }


        view.findViewById<CustomButton>(R.id.splash_btn_not_agreen).setOnClickListener{
            dismiss()
            exitProcess(0)
        }

        return view
    }
}